import { Injectable, NotFoundException } from '@nestjs/common';
import { UpdateFilmPathRelationalDto } from './dto/update-film_path_relational.dto';
import { CreateFilmPathRelationalDto } from './dto/create-film_path_relational.dto';
import { InjectRepository } from '@nestjs/typeorm';
import { FilmPathRelational } from './entities/film_path_relational.entity';
import { Repository } from 'typeorm';
import { PathListService } from 'src/path-list/path-list.service';

@Injectable()
export class FilmPathRelationalService {
  constructor(
    @InjectRepository(FilmPathRelational)
    private readonly filmPathRelationalPaRepository: Repository<FilmPathRelational>,
    private readonly pathListService: PathListService,
  ) {}
  async existsByFilmIdAndPathId(film_id: any, path_id: any) {
    const count = await this.filmPathRelationalPaRepository.count({
      where: {
        film_id: film_id,
        path_id: path_id,
      },
    });

    return count > 0;
  }
  createRelation(createFilmPathRelationalDto: CreateFilmPathRelationalDto) {
    const filmPathrelational = this.filmPathRelationalPaRepository.create(
      createFilmPathRelationalDto,
    );
    this.filmPathRelationalPaRepository.insert(filmPathrelational);
    return '创建关联成功';
  }
  getAll() {
    return this.filmPathRelationalPaRepository.find();
  }
  async getPathByPathIdsAndTypeByFilmId(filmId: string, fileType: string) {
    const pathList = await this.filmPathRelationalPaRepository.find({
      where: { film_id: filmId },
      select: ['path_id'],
    });
    if (pathList.length === 0) return [];
    const pathIdList = pathList.map((item) => item.path_id);
    const fileList = await this.pathListService.getPathByPathIdsAndType(
      fileType,
      pathIdList,
    );
    return fileList;
  }
  async getPathByPathIdsByFilmId(filmId: string) {
    const pathList = await this.filmPathRelationalPaRepository.find({
      where: { film_id: filmId },
      select: ['path_id'],
    });
    if (pathList.length === 0) return [];
    const pathIdList = pathList.map((item) => item.path_id);
    const pathListResult =
      await this.pathListService.getPathByPathIds(pathIdList);
    return pathListResult;
  }
  getRelationByid(id: string) {
    return this.filmPathRelationalPaRepository.findOne({ where: { id: id } });
  }
  // findByFilmId(filmId: string) {
  //   return this.filmPathRelationalPaRepository.findOne({
  //     where: { film_id: filmId },
  //   });
  // }
  findByFilmId(filmId: string) {
    return this.filmPathRelationalPaRepository.find({
      where: { film_id: filmId },
      select: ['path_id'],
    });
  }
  async updateRelationById(
    id: string,
    updateFilmPathRelationalDto: UpdateFilmPathRelationalDto,
  ) {
    const oldItem = await this.filmPathRelationalPaRepository.findOne({
      where: { id },
    });

    if (!oldItem) {
      throw new NotFoundException('该修改对象不存在');
    }

    const updateScenePathRelational = this.filmPathRelationalPaRepository.merge(
      oldItem,
      updateFilmPathRelationalDto,
    );

    await this.filmPathRelationalPaRepository.save(updateScenePathRelational);

    // 返回更新后的实体或其他有用的信息
    return updateScenePathRelational;
  }
  async deleteRelationById(id: string) {
    const result = await this.filmPathRelationalPaRepository.delete({
      id: id,
    });
    if (result.affected && result.affected > 0) {
      return `成功删除 ${result.affected} 条记录`;
    } else {
      return `未找到匹配的记录，没有进行删除操作`;
    }
  }
  async deleteRelationByFilmIdByPathId(pathId: string, filmId: string) {
    const result = await this.filmPathRelationalPaRepository.delete({
      path_id: pathId,
      film_id: filmId,
    });
    if (result.affected && result.affected > 0) {
      return `成功删除 ${result.affected} 条记录`;
    } else {
      return `未找到匹配的记录，没有进行删除操作`;
    }
  }
}
